/**
 * yeahmobi.com Inc.
 * Copyright © 2014 YeahMobi | All Rights Reserved.
 */

package com.ndp.fb.walle.controller;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.rdb.model.Page;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.PageService;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.util.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Title: PageController.java
 * @Copyright: Copyright © 2015
 * @Description:
 * @Company: yeahmobi
 * @Created on 上午10:36:21
 * @author giser.yang
 */
@RequestMapping("/page")
@Controller
public class PageController {
	private static final Logger logger = LoggerFactory
			.getLogger(PageController.class);
	@Autowired
	private PageService pageService;

	/**
	 * 获取主页
	 */
	@RequestMapping(value = "getPages")
	@ResponseBody
	public ResponseEntity<String> getPages(
			HttpServletRequest httpServletRequest) {
		logger.info("PageController getPages() begin.");
		ErrorType errorType = null;
		List<Page> pages = null;
		try {
			User user = (User) httpServletRequest.getSession().getAttribute(
					"user");
			String userId = user.getTicket();
			pages = pageService.getPagesByUserId(userId);
		} catch (Exception e) {
			errorType = ErrorType.INTERNAL_EXCEPTION;
			logger.error(e.getMessage(), e);
		}
		OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType,
				pages);
		logger.info("PageController getPages() end.");
		return ResponseUtils.returnJsonWithUTF8(JSON.toJSON(outputResult)
				.toString());
	}
}
